package com.atguigu.web;

import com.atguigu.pojo.impl.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.test.UserServletTest;
import com.atguigu.utils.WenUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    /**
     * 注销
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1.销毁session中用户登录的信息（或者销毁Session）
        request.getSession().invalidate();
//        2.重定向到首页（或登录页面）
        response.sendRedirect(request.getContextPath());
    }
    //处理登录
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获得请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //2.调用userService.Login()登录处理业务
        User loginUser = userService.login(new User(null, username, password, null));
        //如果等于null，说明登录失败
        if (loginUser == null) {
            //跳回登录页面
            //把错误信息，和回显的表单项信息，保存到Request域中
            request.setAttribute("msg", "用户或密码错误!");
            request.setAttribute("username", username);
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        } else {
            //登录 成功
            request.getSession().setAttribute("user",loginUser);
            //跳到成功页面login_success.html
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request, response);
        }
    }

    //处理注册
    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String token = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        User loginUser = new User(null,username,password,email);
        User user = WenUtils.copyParamToBean(request.getParameterMap(),new User());

        //服务器创建验证码，还没学验证码先写死，要求验证码为 abcde
        if (token != null && token.equalsIgnoreCase(code)) {
            if (userService.existsUsername(username)) {
                System.out.println("用户名[" + username + "]已存在");
                //把回显信息，保存到Request域中
                request.setAttribute("msg", "用户名已存在！！");
                request.setAttribute("username", username);
                request.setAttribute("email", email);
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            } else {
                userService.registUser(new User(null, username, password, email));
                request.getSession().setAttribute("user",loginUser);
                request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request, response);
            }
        } else {
            //把回显信息，保存到Request域中
            request.setAttribute("msg", "验证码错误！！");
            request.setAttribute("username", username);
            request.setAttribute("email", email);
            //验证码错误 跳回注册页面
            System.out.println("验证码[" + code + "]错误");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }
    }


}

